from apps.interface.models.interfaceapimsg import InterfaceApiMsg
from apps.interface.models.interfacecase import InterfaceCase
from apps.auth.models.users import UserBindRole, User
from apps.interface.models.interfaceproject import InterfaceProject


class InterfaceStatistics(object):
    
    @classmethod
    def get_service_case_total(cls, all_project_id, start_time, end_time):
        """
        根据时间、all_project_id获取接口个数
        :param all_project_id: 项目id
        :param start_time:
        :param end_time:
        :return: projects,end_time
        """
        # 查询出所有project_id
        projects = InterfaceProject.query.filter_by(all_project_id=all_project_id,
                                                    status=0).all()
        if not projects:
            return 0
        ret = [{'api_total': len(InterfaceApiMsg.query.
                                 filter(InterfaceApiMsg.project_id == project.id,
                                        InterfaceApiMsg.status == 0,
                                        InterfaceApiMsg.creation_time >= start_time,
                                        InterfaceApiMsg.creation_time <= end_time).all()),
                'case_total': len(InterfaceCase.query.
                                  filter(InterfaceCase.project_id == project.id,
                                         InterfaceCase.status == 0,
                                         InterfaceCase.creation_time >= start_time,
                                         InterfaceCase.creation_time <= end_time).all()),
                'project_name': project.name} for project in projects]
        
        return ret
    
    @classmethod
    def get_user_case_total(cls, all_project_id, start_time, end_time):
        """
        根据时间、all_project_id获取接口个数
        :param all_project_id:
        :param start_time:
        :param end_time:
        :return:
        """
        # 过滤全部为测试人员
        users = UserBindRole.query.filter_by(role_id=UserBindRole.TestRole,
                                             project_id=all_project_id).all()
        
        project_id = [i.id for i in
                      InterfaceProject.query.filter_by(all_project_id=all_project_id,
                                                       status=0).all()]
        if not users:
            return 0
        # api、case数量
        ret = [{'api_total': len(InterfaceApiMsg.query.
                                 filter(InterfaceApiMsg.user_id == user.user_id,
                                        InterfaceApiMsg.project_id.in_(project_id),
                                        InterfaceApiMsg.status == 0,
                                        InterfaceApiMsg.creation_time >= start_time,
                                        InterfaceApiMsg.creation_time <= end_time).all()),
                'case_total': len(InterfaceCase.query.
                                  filter(InterfaceCase.user_id == user.user_id,
                                         InterfaceCase.project_id.in_(project_id),
                                         InterfaceCase.status == 0,
                                         InterfaceCase.creation_time >= start_time,
                                         InterfaceCase.creation_time <= end_time).all()),
                # 遍历user.user_id 查user_name
                'user_name': User.query.filter(User.id == user.user_id).first().nickname}
               for user in users if User.query.filter(User.id == user.user_id).first()]
        
        return ret
    
    # 先备用
    @classmethod
    def get_all_user_case_total(cls, start_time, end_time):
        """
        根据时间获取接口个数
        :param start_time:
        :param end_time:
        :return:
        """
        # 过滤全部为测试人员
        users = UserBindRole.query.filter_by(role_id=3).group_by(UserBindRole.user_id).all()
        if not users:
            return 0
        # api、case数量
        ret = [{'api_total': len(InterfaceApiMsg.query.
                                 filter(InterfaceApiMsg.user_id == user.user_id,
                                        InterfaceApiMsg.status == 0,
                                        InterfaceApiMsg.creation_time >= start_time,
                                        InterfaceApiMsg.creation_time <= end_time).all()),
                'case_total': len(InterfaceCase.query.
                                  filter(InterfaceCase.user_id == user.user_id,
                                         InterfaceCase.status == 0,
                                         InterfaceCase.creation_time >= start_time,
                                         InterfaceCase.creation_time <= end_time).all()),
                # 遍历user.user_id 查user_name
                'user_name': User.query.filter(User.id == user.user_id).first().nickname}
               for user in users if User.query.filter(User.id == user.user_id).first()]
        return ret
